package org.chs.ts.batch.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.chs.ts.base.batch.del.entity.BatchClearBasicTaskStepExecDel;
import org.chs.ts.batch.mapper.BatchClearBasicTaskStepExecDelMapper;
import org.chs.ts.batch.util.ConvertToUtil;
import org.chs.ts.batch.service.IBatchClearBasicTaskStepExecDelService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: batch_clear_basic_task_step_exec_del
 * @Author: jeecg-boot
 * @Date: 2024-04-09
 * @Version: V1.0
 */
@Service
@Slf4j
public class BatchClearBasicTaskStepExecDelServiceImpl extends ServiceImpl<BatchClearBasicTaskStepExecDelMapper, BatchClearBasicTaskStepExecDel> implements IBatchClearBasicTaskStepExecDelService {

    private final String CLASS_NAME = this.getClass().getSimpleName();

    @Resource
    private BatchClearBasicTaskStepExecDelMapper mapper;

    @Override
    public boolean saveBatchs(List<BatchClearBasicTaskStepExecDel> taskStepDelist) {
        boolean result = false;
        List<BatchClearBasicTaskStepExecDel> rollbackList = new ArrayList<>();
        String ENTITY_NAME = "BatchClearBasicTaskStepExecDel";
        for (BatchClearBasicTaskStepExecDel item : taskStepDelist) {
            result = mapper.insert(item) > 0;

            if (!result) {
                log.info(CLASS_NAME + " -> saveBatchs insert " + ENTITY_NAME + " exception:" + ENTITY_NAME + " -> " + ConvertToUtil.printObjectInfo(item, new ArrayList<>()));
                continue;
            }

            rollbackList.add(item);

        }

        if (!result) {
            log.info(CLASS_NAME + " -> saveBatchs rollabck ");
            for (BatchClearBasicTaskStepExecDel item : rollbackList) {
                if (mapper.deleteById(item) <= 0) {
                    log.info(CLASS_NAME + " -> saveBatchs rollback " + ENTITY_NAME + " exception:" + ENTITY_NAME + " -> " + ConvertToUtil.printObjectInfo(item, new ArrayList<>()));
                }
            }
        }

        return result;
    }
}
